/* -----------------------------------------------------------------------------
 * PROJECT    : net.lwf
 * FILE       : TimeWordProblemFactory.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Jan 28, 2009  Raghu Sarraff	 		        Created
 */
package lwf.factory;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import net.lwf.exception.AppException;
import net.lwf.exception.ExceptionUtil;


public class TimeWordProblemFactory {

	private static TimeWordProblemFactory instance = null;
	
	private ResourceBundle timeWpBundle = null;
	
	private Map mapTimeWp = null;
    	
	private TimeWordProblemFactory() {
        init();
	}
	
	public String getTimeWp(int questionkey) {
		if(mapTimeWp == null) {
			process(timeWpBundle);
		}
		return (String) mapTimeWp.get(String.valueOf(questionkey));
	}
	
	public int getWpCount() {
		if(mapTimeWp == null) {
			this.process(timeWpBundle);
		}
		return mapTimeWp.size();
	}
	
	public synchronized final static TimeWordProblemFactory getInstance(){
		if (instance == null) {
			instance = new TimeWordProblemFactory();
		}
		return instance;
	}
	
	private void init() {
		if(timeWpBundle == null) {
			try {
				timeWpBundle = ResourceBundle.getBundle("general.timewordproblems");
			}
			catch(MissingResourceException mrex) {
				//throw new AppException(mrex);
				ExceptionUtil.logAndThrowException(TimeWordProblemFactory.class, mrex);
			}
		}
	}
	
	private void process(ResourceBundle resource) {
		mapTimeWp = new HashMap();
		Enumeration enum1 = resource.getKeys();
		while(enum1.hasMoreElements()) {
			String key = enum1.nextElement().toString();
			mapTimeWp.put(key, resource.getString(key));
		}
	}
	
}

